769. 最多能完成排序的块
为保证权益,题目请参考 769. 最多能完成排序的块(From LeetCode).
解决方案1
Python
python
# 769. 最多能完成排序的块
# https://leetcode.cn/problems/max-chunks-to-make-sorted/
from typing import List
class Solution:
def maxChunksToSorted(self, arr: List[int]) -> int:
count = 0
ri = 0
for i, a in enumerate(arr):
if a > ri:
ri = a
if i == ri:
count += 1
ri = i + 1
return count
if __name__ == "__main__":
so = Solution()
assert so.maxChunksToSorted([4, 3, 2, 1, 0]) == 1
assert so.maxChunksToSorted([1, 0, 2, 3, 4]) == 4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27